package com.person.tbsystem.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.person.tb.face.OnViewChangeListener;
import com.person.tb.widgets.MyScrollLayout;
import com.person.tbsystem.R;
import com.person.tbsystem.globarVar.TbConstants;
import com.person.tbsystem.utlis.SharedPreferencesUtils;

/**
 * Created by Demon on 2015/12/23.
 * 新手指引界面
 */
public class GuideActivity extends Activity implements OnViewChangeListener, View.OnClickListener {

    private MyScrollLayout mScrollLayout;

    private ImageView[] mImageViews;

    private int mViewCount;

    private int mCurSel;
    private Button btn_use;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide);
        init();
    }

    private void init() {
        mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout);

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llayout);
        btn_use = (Button) findViewById(R.id.btn_use);
        mViewCount = mScrollLayout.getChildCount();
        mImageViews = new ImageView[mViewCount];

        for (int i = 0; i < mViewCount; i++) {
            mImageViews[i] = (ImageView) linearLayout.getChildAt(i);
            mImageViews[i].setEnabled(true);
            mImageViews[i].setOnClickListener(this);
            mImageViews[i].setTag(i);
        }

        mCurSel = 0;
        mImageViews[mCurSel].setEnabled(false);

        mScrollLayout.SetOnViewChangeListener(this);
        SharedPreferencesUtils.setParam(this, TbConstants.FIRST_OPEN, true);
        btn_use.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent guideIntent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(guideIntent);
                finish();
            }
        });
    }

    private void setCurPoint(int index) {
        if (index < 0 || index > mViewCount - 1 || mCurSel == index) {
            return;
        }

        mImageViews[mCurSel].setEnabled(true);
        mImageViews[index].setEnabled(false);

        mCurSel = index;
    }

    @Override
    public void onClick(View v) {
        int pos = (Integer) (v.getTag());
        setCurPoint(pos);
        mScrollLayout.snapToScreen(pos);
    }

    @Override
    public void OnViewChange(int view) {
        if (view==4){
            btn_use.setVisibility(View.VISIBLE);
        }else {
            btn_use.setVisibility(View.GONE);
        }
        setCurPoint(view);
    }
}
